package com.ly.heart.service.impl;

import java.util.List;
import java.util.Optional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.domain.ActCurrentTaskVo;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.ActTaskCommentEntity;
import com.ly.heart.domain.dto.MaterialDetailDto;
import com.ly.heart.domain.dto.OrderQueryDto;
import com.ly.heart.domain.dto.SaleOrderQueryDto;
import com.ly.heart.domain.dto.SaleQuotationDetailDto;
import com.ly.heart.domain.vo.*;
import com.ly.heart.service.IActModelService;
import com.ly.heart.service.IActTaskCommentService;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.MaterialDetailMapper;
import com.ly.heart.domain.MaterialDetailEntity;
import com.ly.heart.service.IMaterialDetailService;

import javax.annotation.Resource;

/**
 * 采购订单/销售订单/出入库清单明细Service业务层处理
 * @author cxl
 * @date 2024-05-14
 */
@Service
public class MaterialDetailServiceImpl extends ServiceImpl<MaterialDetailMapper, MaterialDetailEntity> implements IMaterialDetailService {

    @Resource
    private MaterialDetailMapper materialDetailMapper;
    @Resource
    private IActModelService iActModelService;
    @Resource
    private IActTaskCommentService actTaskCommentService;

    /**
     * 查询采购订单/销售订单/出入库清单明细
     * @param id 采购订单/销售订单/出入库清单明细主键
     * @return 采购订单/销售订单/出入库清单明细
     */
    @Override
    public MaterialDetailEntity selectOrderWarehouseDetailEntityById (String id) {
        return materialDetailMapper.selectOrderWarehouseDetailEntityById ( id );
    }

    /**
     * 查询采购订单/销售订单/出入库清单明细列表
     * @param materialDetailEntity 采购订单/销售订单/出入库清单明细
     * @return 采购订单/销售订单/出入库清单明细
     */
    @Override
    public List<MaterialDetailEntity> selectOrderWarehouseDetailEntityList (MaterialDetailEntity materialDetailEntity) {
        return materialDetailMapper.selectOrderWarehouseDetailEntityList ( materialDetailEntity );
    }

    /**
     * 新增采购订单/销售订单/出入库清单明细
     * @param materialDetailEntity 采购订单/销售订单/出入库清单明细
     * @return 结果
     */
    @Override
    public int insertOrderWarehouseDetailEntity (MaterialDetailEntity materialDetailEntity) {
        materialDetailEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        materialDetailEntity.setCreateTime ( DateUtils.getNowDate () );
        return materialDetailMapper.insertOrderWarehouseDetailEntity ( materialDetailEntity );
    }

    /**
     * 修改采购订单/销售订单/出入库清单明细
     * @param materialDetailEntity 采购订单/销售订单/出入库清单明细
     * @return 结果
     */
    @Override
    public int updateOrderWarehouseDetailEntity (MaterialDetailEntity materialDetailEntity) {
        return materialDetailMapper.updateOrderWarehouseDetailEntity ( materialDetailEntity );
    }

    /**
     * 批量删除采购订单/销售订单/出入库清单明细
     * @param ids 需要删除的采购订单/销售订单/出入库清单明细主键
     * @return 结果
     */
    @Override
    public int deleteOrderWarehouseDetailEntityByIds (String[] ids) {
        return materialDetailMapper.deleteOrderWarehouseDetailEntityByIds ( ids );
    }

    /**
     * 删除采购订单/销售订单/出入库清单明细信息
     * @param id 采购订单/销售订单/出入库清单明细主键
     * @return 结果
     */
    @Override
    public int deleteOrderWarehouseDetailEntityById (String id) {
        return materialDetailMapper.deleteOrderWarehouseDetailEntityById ( id );
    }

    @Override
    public IPage<MaterialDetailVo> selectInOurStorageMaterialDetailVoList(MaterialDetailDto materialDetailDto) {
        return materialDetailMapper.selectMaterialDetailVoList(materialDetailDto);
    }

    @Override
    public List<OrderVo> getOrderPageListByMaterial(OrderQueryDto queryDto) {
        return materialDetailMapper.getOrderPageListByMaterial(queryDto);
    }

    @Override
    public Integer queryTotalCount(OrderQueryDto queryDto) {
        return materialDetailMapper.queryTotalCount(queryDto);
    }

    @Override
    public List<OrderChangeVo> getOrderChangeListByMaterial(OrderQueryDto queryDto) {
        return materialDetailMapper.getOrderChangeListByMaterial(queryDto);
    }

    @Override
    public  IPage<SaleOrderChangeVo> selectSaleOrderChangeListByMaterial(SaleOrderQueryDto queryDto) {
        return materialDetailMapper.selectSaleOrderChangeListByMaterial(queryDto);
    }

    @Override
    public IPage<SaleQuotationDetailVo> selectSaleQuotationDetailVoList(SaleQuotationDetailDto saleQuotationDetailDto) {
        IPage<SaleQuotationDetailVo> saleQuotationDetailVoIPage = materialDetailMapper.selectSaleQuotationDetailVoList(saleQuotationDetailDto);
        saleQuotationDetailVoIPage.getRecords().forEach(saleQuotationDetailVo -> {
            //审批人信息
            ActTaskCommentEntity actTaskComment = actTaskCommentService.queryLastComment(saleQuotationDetailVo.getInstanceId());
            Optional.ofNullable(actTaskComment).ifPresent(taskComment -> {
                saleQuotationDetailVo.setApprovedUser(taskComment.getApprovedUser());
                saleQuotationDetailVo.setRemark(taskComment.getRemark());
            });
            ActCurrentTaskVo currentTaskVo = iActModelService.getCurrentTaskByInstanceId(saleQuotationDetailVo.getInstanceId());
            Optional.ofNullable(currentTaskVo).ifPresent(currentTask -> {
                saleQuotationDetailVo.setCurrentTask(currentTask);
            });
        });
        return saleQuotationDetailVoIPage;
    }

    @Override
    public IPage<SaleQuotationDetailVo> selectMenuSaleQuotationDetailVoList(SaleQuotationDetailDto saleQuotationDetailDto) {
        IPage<SaleQuotationDetailVo> saleQuotationDetailVoIPage = materialDetailMapper.selectMenuSaleQuotationDetailVoList(saleQuotationDetailDto);
        return saleQuotationDetailVoIPage;
    }

}
